import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;

public class Group {
	private String id;
	private Tutor tutor;
	private int maxStudents;
	private HashMap<String, Student> students;
	private HashMap<GregorianCalendar, Excercise> excercises;
	private HashMap<Student, HashMap<GregorianCalendar, Excercise>> achievedExcercises;

	public Group(String id, Tutor tutor, int maxStudents) {
		this.id = id;
		this.tutor = tutor;
		this.maxStudents = maxStudents;
	}
	
	public Group(String id, Tutor tutor) {
		this.id = id;
		this.tutor = tutor;
		this.maxStudents = 0;
	}
	
	public Group(String id, int maxStudents) {
		this.id = id;
		this.tutor = tutor;
		this.maxStudents = maxStudents;
	}
	
	public Group(String id) {
		this.id = id;
	}
	
	/**
	 * @return the id
	 */
	public String getID() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setID(String ID) {
		this.id = ID;
	}
	
	/**
	 * @return the tutor
	 */
	public Tutor getTutor() {
		return tutor;
	}

	/**
	 * @param tutor the tutor to set
	 */
	public void setTutor(Tutor tutor) {
		this.tutor = tutor;
	}
	
	public boolean removeTutor(Tutor tutor) {
		if (this.getTutor().getId().equals(tutor.getId())) {
			this.tutor = null;
			return true;
		}
		return false;
	}
	
	public boolean swapTutor(Tutor tutor1, Tutor tutor2) {
		if (this.getTutor().getId().equals(tutor1.getId())) {
			this.tutor = tutor2;
			return true;
		}
		return false;
	}

	/**
	 * @return the maxStudents
	 */
	public int getMaxStudents() {
		return maxStudents;
	}

	/**
	 * @param maxStudents the maxStudents to set
	 */
	public void setMaxStudents(int maxStudents) {
		this.maxStudents = maxStudents;
	}

	/**
	 * @return the students
	 */
	public HashMap<String, Student> getStudents() {
		return students;
	}

	/**
	 * @param students the students to set
	 */
	public void setStudents(HashMap<String, Student> students) {
		this.students = students;
	}
	
	public int getNumberOfStudents(){
		return students.size();
	}
	
	public boolean addStudent(Student student) {
		if (maxStudents == 0 || this.getNumberOfStudents() < this.getMaxStudents()) {
			if (!this.getStudents().containsKey(student.getMatnr())) {
				this.getStudents().put(student.getMatnr(), student);
				return true;
			}
		}
		return false;
	}
	
	public boolean removeStudent(Student student) {
		if (this.getStudents().containsKey(student.getMatnr())) {
			this.getStudents().remove(student.getMatnr());
			return true;
		}
		return false;
	}
	
	public boolean addExcercise(Excercise excercice) {
		if (!this.excercises.containsKey(excercice.getDate())) {
			this.addExcercise(excercice);
		}
		return false;
	}
	
	public boolean addExcercise(GregorianCalendar date, Points points) {
		if (!this.excercises.containsKey(date)) {
			this.addExcercise(new Excercise(date, points));
		}
		return false;
	}
	
	public boolean addExcercise(GregorianCalendar date, int maxPoints) {
		if (!this.excercises.containsKey(date)) {
			this.addExcercise(new Excercise(date, new Points(maxPoints)));
		}
		return false;
	}
	
	public void addAchievedExcercises(Student student, Excercise excercise, int achievedPoints) {
		if (!this.achievedExcercises.containsKey(student)) {
			this.achievedExcercises.put(student, new HashMap<GregorianCalendar, Excercise>());
		}
		this.achievedExcercises.get(student).put(excercise.getDate(), new Excercise(excercise, achievedPoints));
	}
	
	public int getAchievedPoints(Student student) {
		int totalPoints = 0;
		HashMap<GregorianCalendar, Excercise> studentsAchievedExcercises = this.achievedExcercises.get(student);
		
		Iterator it = studentsAchievedExcercises.keySet().iterator();
			while(it.hasNext()) {
				GregorianCalendar key = (GregorianCalendar)it.next();
				Excercise excercise = studentsAchievedExcercises.get(key);
				totalPoints += excercise.getPoints().getAchievedPoints();
			}
		return totalPoints;
	}
}